
var frontBaseUrl = "http://localhost:5500";
var backBaseUrl = "http://localhost:8080";
        
var chinese_ptn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
var space_ptn = /\s/g;

function check_username(){
	let str = $("#username").val();
	console.log(213)
	if(str.length < 3 || str.length > 25){
		$("#username-item .check-tipper").text("3~25位字母数字或下划线，首位非数字")
		return false;
	}
	
	let username_ptn = /^[a-zA-Z_][a-zA-Z0-9_]{2,24}/
	if(chinese_ptn.test(str) || space_ptn.test(str) || !username_ptn.test(str)){
		console.log("fal")
		$("#username-item .check-tipper").text("3~25字符，a~z,A~Z,_首位非数字")
		return;
	}
	$("#username-item .check-tipper").text("")
	$.ajax({
		url: backBaseUrl + '/regis/username-unique',
		method: 'POST',
		data: {
			"username":str
		},
		crossDomain: true,
		xhrFields: { withCredentials: true },
		success: function (msg) {
			let data = msg.data.unique;
			if(data == false){
				$("#username-item .check-tipper").html("").append("该用户名已存在！")
				$("#username-item .valid-checker").html("x");
				return false;
			}else{
				$("#username-item .check-tipper").html("").append("该名称可用");
				$("#username-item .valid-checker").html("v")
				return true;
			}
		}, error: function (error) {
			return false;
		}
	});
	
}

function password_level_check(){
	//长度不够8位，无反应
	//长度超过30位，不能输入
	//超过八位，大小写、（点和横杠下划线）三种的一种
	//验证长度超过8位，且包括两种，中
	//三种，高
	let pwd = $("#password").val();
	console.log(pwd);
	let pwd_ptn = /\w{8,30}/
	if(!pwd_ptn.test(pwd)){
		$("#pwd-item .check-tipper").text("8~30位，字母数字或下划线")
	$("#pwd-item .valid-checker").text("x")

		return false;
	}

	$("#pwd-item .check-tipper").text("")
	$("#pwd-item .valid-checker").text("v")
	
	let isUpperExisted = 0;
	let isLowerExisted = 0;
	let isSpecialLetterExisted = 0;
	let isDigitExisted = 0;
	//遍历字符串
	for(let i = 0;i < pwd.length;i++){
		if(pwd[i] >= 'a' && pwd[i] <= 'z'){
			isLowerExisted = 1;
		} else
		if(pwd[i] >= 'A' && pwd[i] <= 'Z'){
			isUpperExisted = 1;
		} else
		if(pwd[i] == '.' || pwd[i] == '_' || pwd[i] == '-'){
			isSpecialLetterExisted = 1;
		} else if(pwd[i] >= '0' && pwd[i] <= '9'){
			isDigitExisted = 1;
		}else
		{//不是的不能输入，去掉
			pwd = pwd.substring(0,pwd.length - 1);
			$("#password").val(pwd);
			return false;
		}
	}
	let strong = isDigitExisted + isUpperExisted + isLowerExisted + isSpecialLetterExisted;
	$("#pwd-lvl").css("width",(strong * 50) + "px")
	return pwd.length >= 8;

}

function valid_password(){
	let pwd = $("#pwd-item input").val();
	let rep_pwd = $("#repeat-pwd-item input").val();
	if(rep_pwd.length > 30){
		$("#repeat-pwd-item input").val(rep_pwd.substring(0,rep_pwd.length-1));
	}
	if(pwd === rep_pwd){
		$("#repeat-pwd-item .valid-checker").text("v");
		$("#repeat-pwd-item .check-tipper").text("");

		return true;
	}else{
		$("#repeat-pwd-item .valid-checker").text("x");
		$("#repeat-pwd-item .check-tipper").text("两次输入不一致");

		return false;
	}
	
}

function email_check(){
	// 常见的有 @qq.com、@163.com、@139.com、
	//@sohu.com、@aliyun.com、@189.com、
	//@hotmail.com、@gmail.com、@sina.com、
	//@yahoo.com @gmail.com
	//数字、字母、点 .com
	let email_ptn = /[a-zA-Z0-9.-_]+@[0-9a-z\.]+(\.com)$/
	let email = $("#email-item input").val();
	console.log(email)
	if(!email_ptn.test(email) || chinese_ptn.test(email) || space_ptn.test(email)){
		console.log("fail");
		$("#email-item .check-tipper").text("请输入格式正确的邮箱地址");
		$("#email-item .valid-checker").text("x");
		return false;
	}
	
	$("#email-item .check-tipper").text("");
	$("#email-item .valid-checker").text("v");
	return true;
}

function send_valid_email(){
	if(!password_level_check() || !valid_password() || !email_check()){
		window.alert("请输入正确的信息");
		return;
	}
	let btn = $("#send-email-btn");
	let time = 30;
	btn.val("重新发送(" + time + ")" );

	btn.attr("disabled","disabled")
	sendEmail();
	let timer = setInterval(function(){
		if(time == 0){
			clearInterval(timer);
			btn.attr("disabled",false);
			btn.val("发送验证码");
			time = 30;
		}else{
			time--;
			btn.val("重新发送(" + time + ")" );
		}
		console.log(time)
	},1000)
}

function sendEmail(){
	
	let email = $("#email-item input").val();
	$.ajax({
		url: backBaseUrl + "/email/verify",
		crossDomain: true,
		xhrFields: { withCredentials: true },
                
		data: {
			"to_email":email
		},
		method: "POST",
		success: function(r){
			console.log(r);
		}
	})
}

function register(){
	let v = $("#reg-form").serialize();
	console.log(v);
	$.ajax({
		url: backBaseUrl + "/regis/register",
		data: v,
		method: "POST",
		crossDomain: true,
		xhrFields: { withCredentials: true },
		success: function(r){
			console.log(r);
			if(r.code === -1){
				layer.msg(r.msg);
				return;
			}

			window.location.href = "login.html";
		},error(error){
			console.log(error);
		}
	})
}
